<?php
class User extends AppModel {
    public $name         = 'User';
    public $displayField = 'fullname';
    public $validate     = array(
        'username' => array(
            'rule1' => array(
                'rule'     => array('between', 5, 50),
                'message'  => 'Username must be between 5 and 50 characters long',
                'required' => true
            ),
            'rule2' => array(
                'rule'    => 'isUnique',
                'message' => 'This username has already been taken'
            )
        ),
        'password' => array(
            'rule'     => array('between', 5, 50),
            'message'  => 'Password must be between 5 and 50 characters long',
            'required' => true,
        ),
        'fullname' => array(
            'rule'     => array('between', 2, 100),
            'message'  => 'Fullname must be between 2 and 100 characters long',
            'required' => true
        ),
        'level' => array(
            'rule'     => 'notEmpty',
            'required' => true
        )
    );
    public $belongsTo = array(
        'Hotel' => array(
            'className' => 'Hotel'
        )
    );

    public function usertList ($params) {
        $page = $params['page'];
        $cond = 'u.`status`="A" ';

        if ($params['query'] && $params['qtype']) {
            $cond .= 'AND u.`'.$params['qtype'].'` LIKE "%'.$params['query'].'%" ';
        }

        $sql = 'SELECT COUNT(*) AS total FROM `users` AS u ' .
               'WHERE '.$cond;
        $ttl = $this->query($sql);

        $total = (count($ttl) == 0) ? 0 : $ttl[0][0]['total'];
        if ($total == 0) {
            return array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );
        }

        $rpp   = isset($params['rp']) ? $params['rp'] : 10;
        $start = (($page-1) * $rpp);
        $sort  = empty($params['sortname']) ? 'id' : $params['sortname'];
        $ord   = (empty($params['sortorder']) || $params['sortorder'] == 'asc') ? 'ASC' : 'DESC';
        $sql   = 'SELECT u.* FROM `users` AS u ' .
                 'WHERE '.$cond .
                 'ORDER BY u.`'.$sort.'` '.$ord.' ' .
                 'LIMIT '.$start.', '.$rpp;
        $data = $this->query($sql);
        $rec  = array();

        if (count($data) == 0) {
            return array(
                'page'  => 1,
                'total' => 0,
                'rows'  => array()
            );
        }

        foreach ($data as $rs) {
            $id = $rs['u']['id'];

            $lastLogin = $this->serverToClientTime($rs['u']['last_login'], 'd/m/Y H:i a');
            $created   = $this->serverToClientTime($rs['u']['created'], 'd/m/Y H:i a');
            $modified  = $this->serverToClientTime($rs['u']['modified'], 'd/m/Y H:i a');
            $actions   = '<div class="selectRecord" onclick="selectUserRecord('.$id.');"></div>' .
                         '<div class="editRecord" onclick="editUserProfile('.$id.');"></div>';

            $cell = array(
                $id, $rs['u']['username'], $rs['u']['password'], $rs['u']['fullname'], $rs['u']['position'], $rs['u']['level'], $lastLogin, $created, $modified, $actions
            );

            $rec[] = array(
                'id'   => $id,
                'cell' => $cell
            );
        }

        return array(
            'page'  => $page,
            'total' => $total,
            'rows'  => $rec
        );
    } // usertList
} // User
?>